package com.kono.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kono.entity.PageResult;
import com.kono.mapper.StuMapper;
import com.kono.mapper.TeachMapper;
import com.kono.pojo.Stu;
import com.kono.pojo.Teach;
import com.kono.service.TeachService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeachServiceImpl extends ServiceImpl<TeachMapper,Teach> implements TeachService {

    @Autowired
    TeachMapper teachMapper;

    @Autowired
    StuMapper stuMapper;

    @Override
    public PageResult<Teach> findPage(Teach teach, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<Teach> list = teachMapper.findPage(teach);
        PageInfo<Teach> pageInfo = new PageInfo<>(list);
        return new PageResult<Teach>(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public void add(Teach teach) {
        teachMapper.insert(teach);

        List<Stu> stus = teach.getStus();
        if (stus!=null){
            for (Stu stu : stus) {
                if (stu.getName()!=null){
                    System.out.println("------------"+stu+"--------------");
                    stuMapper.insert(stu);
                }
            }
        }
    }
}
